#!/usr/bin/python3
# -*- coding: utf-8 -*-

import random

class foodList(object):
    def __init__(self):
        self._foodList = []

    def clean(self):
        self._foodList = []

    def add(self, food):
        self._foodList.append(food)

    def dump(self):
        num = 1
        for food in self._foodList:
            print('(%d):%s' % (num, food))
            num += 1

    def delete(self, num):
        try:
            self._foodList.pop(num)
            return True
        except IndexError:
            return False

    def getLen(self):
        return len(self._foodList)

    def choose(self):
        a   =   0
        b   =   self.getLen() - 1
        num = random.randint(a, b)
        print('就选%s吧' % self._foodList[num])

###控制台类，用于处理命令###
class console(object):
    def __init__(self):
        self._redo = 1
        self._list = foodList()

    def bye(self):
        self._redo = 0
        input('拜拜~(按Enter键退出)')

    def restart(self):
        self._redo = 0
        import sys
        import os
        command = 'start python '+sys.argv[0]
        os.system(command)

    def what(self):
        if self._list.getLen():
            self._list.choose()
        else:
            print('没有选项怎么选啊，试着make一下')

    def make(self):
        if self._list.getLen():
            confirm = input('要清空原列表嘛?(y/n)')
            if confirm == 'y':
                self._list.clean()
        while 1:
            result = self._addend()
            if not result:
                if self._list.getLen():
                    print('生成列表成功~')
                return

    def _addend(self, food=None):
        foodNum = self._list.getLen() + 1
        if food==None:
            food    = input('选项(%s):' % foodNum)
        food    = str(food)
        if len(food):
            self._list.add(food)
            return True
        else:
            return False

    def add(self):
        result = self._addend()
        if result:
            print('添加成功~')
        return

    def delete(self):
        if not self._list.getLen():
            print('现在没有任何选项哦……')
            return
        self._list.dump()
        num = input('删除哪项?输入编号:')
        if num == '':
            return
        try:
            num = int(num)
        except ValueError:
            print('要输入正确的数字哦')
            return
        result = self._list.delete(num-1)
        if result:
            print('删除成功~')
        else:
            print('没有这个选项哎……')

    def console(self):
        while self._redo:
            re = input('命令>')
            re = re.lower()
            if not len(re):
                continue
            if re.find('_') == 0:
                print('不可以输入奇怪的东西哦……')
                continue
            try:
                re = eval("self."+re)
                re()
            except:
                print('这是什么…你想表达什么…')


console = console()
console.console()

